問題の説明
Google アクセス トークンを使用してユーザー プロファイルを取得するにはどうすればよいですか (How do I get user profile using Google Access Token)
i'm testing getting user information by google access token
http://www.mawk3y.net/glogin
after clicking sign in button i get redirected to
https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=access_token_here
And get some JSON data like this
{
"issued_to": "my client id.apps.googleusercontent.com",
"audience": "my client id.apps.googleusercontent.com",
"user_id": "user id here",
"scope": "https://www.googleapis.com/auth/plus.login",
"expires_in": 3596,
"access_type": "online"
}
now i need to know how to extract user name , address and email any help please ?
thanks in advance
リファレンスソリューション
方法 1:
Try this one:
var url = 'https://www.googleapis.com/plus/v1/people/me?access_token={access_token}';
$.ajax({
type: 'GET',
url: url,
async: false,
success: function(userInfo) {
//info about user
console.log(userInfo);
console.log('test');
},
error: function(e) {
console.log('error');
}
});
方法 2:
You can verify the auth token received after google signin on your server using this api
Request
https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={accces_token}
Response
{
"email_verified": "true",
"email": "abhinav.xxx@gmail.com",
"name": "abhinav srivastava",
"picture": "https://lh3.googleusercontent.com/‑xgD_zFj1EgY/AAAAAAAAAAI/AAAAAAAACZ0/fnecSQ03o0Y/s96‑c/photo.jpg",
"given_name": "abhinav",
"family_name": "srivastava",
"locale": "en",
...
...
}
source
方法 3:
I had the same issue. I wanted to extract the user information. But couldn't get the exact link to hit. Then I went through the code for Passport Google Strategy at Line number 54
.
My scopes were ['profile', 'email']
GET Request
https://www.googleapis.com/oauth2/v3/userinfo?access_token={access_token}
Response
{
"sub": "23423....",
"name": "John Doe",
"given_name": "John",
"family_name": "Doe",
"picture": "<Profile picture URL>",
"email": "john.doe@gmail.com",
"email_verified": true,
"locale": "en"
}
方法 4:
You need to use the access token (you get it in the redirect url) to access Google's People API. Check out the specs here.
You might find Google's OAuth2 playground pretty useful to get an idea of how to use access tokens to access Google's APIs.
Good luck!
方法 5:
If you want token_info, pass tokeninfo as param
https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=ya29.a0AfH6SMArZZITzn‑...
If you want all user info, pass userinfo as param
https://www.googleapis.com/oauth2/v3/userinfo?access_token=ya29.a0AfH6SMArZZITzn‑...
tokeninfo reponse
{
"azp": "",
"aud": "",
"sub": "",
"scope": "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile openid",
"exp": "",
"expires_in": "",
"email": "",
"email_verified": "",
"access_type": ""
}
userinfo reponse
{
"sub": "",
"name": "",
"given_name": "",
"family_name": "",
"picture": "",
"email": "",
"email_verified": ,
"locale": ""
}
Don't fogot to pass https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile as scope
var params = {
'client_id': '',
'redirect_uri': '',
'response_type': 'token',
'scope': 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile',
};
(by jq beginner、betmakh、HimalayanCoder、Asutosh Panda、Miguel Andres、Arunjith R S)